Dekoratives Banner

Email Methods


    This script creates an email object for use with the Render and Email script, described above. It uses code which is specific to the socket object and therefore requires advanced understanding of networking to edit; the comments below describe its operation.

    // Create an email object. The function may be called both
    // as a global function and as a constructor. It takes the
    // name of the email server, and an optional Boolean that,
    // if true, prints debugging messages.
    // This object is not guaranteed to work for all SMTP servers,
    // some of them may require a different set of commands.
    // functions:
    // send (fromAddress, toAddress, subject, text) - send an email
    // auth (name, pass) - do an authorization via POP3
    // both functions return false on errors
    // sample:
    // e = new EmailSocket ("mail.host.com");
    // authorize via POP3 (not all servers require authorization)
    // e.auth ("myname", "mypass");
    // send the email
    // e.send ("me@my.com", "you@you.com", "My Subject", "Hi there!")
    // This script makes use of the Socket object, and creates a new class
    // called EmailSocket that is derived from Socket. For more information on
    // creating new classes in this way, consult chapter 7 of JavaScript, The
    // Definitive Guide, by David Flanagan (O'Reilly).
    //This is the constructor for the email socket. It takes as arguments:
    //server - the address of the email server (is not checked for validity here)
    //dbg - a boolean, if true, prints additional error information
    function EmailSocket (server, dbg) {
        var obj = new Socket;
        obj._host = server;
        obj._debug = (dbg == true);
        obj.__proto__ = EmailSocket.prototype;
        return obj;
    }
    // correct the protoype chain to point to the Socket prototype chain
    // - this is what actually causes the derivation from Socket.
    EmailSocket.prototype.__proto__ = Socket.prototype;
    // This sets up the send() member function. send() takes as arguments:
    // from - the email address of the sender. This is not validated.
    // to - the email address of the recipient. If there is an error,
    // and the from address is incorrect, you will not be notified.
    // subject - the contents of the subject field.
    // text - the body of the message.
    //
    // Returns:
    // true if sending succeeded
    // false otherwise (if there was an error)
    //
    // Note that this code uses a local function object to create
    // the function that is assigned to send.
    EmailSocket.prototype.send = function (from, to, subject, text) {
        // open the socket on port 25 (SMTP)
        if (!this.open (this._host + ":25"))
            return false;
        try {
            // discard the greeting
            var greeting = this.read();
            if (this._debug)
                write ("RECV: " + greeting);
            // issue EHLO and other commands
            this._SMTP ("EHLO " + from);
            this._SMTP ("MAIL FROM: " + from);
            this._SMTP ("RCPT TO: " + to);
            this._SMTP ("DATA");
            // send subject and time stamp
            this.writeln ("From: " + from);
            this.writeln ("To: " + to);
            this.writeln ("Date: " + new Date().toString());
            if (typeof subject != undefined)
                this.writeln ("Subject: " + subject);
            this.writeln();
            // send the text
            if (typeof text != undefined)
                this.writeln (text);
            // terminate with a single dot and wait for response
            this._SMTP (".");
            // terminate the session
            this._SMTP ("QUIT");
            this.close();
            return true;
        }
        catch (e) {
            this.close();
            return false;
        }
    }
    // Authorize via POP3. Supply name and password.
    //
    // Returns:
    // true if sending succeeded
    // false otherwise (if there was an error)
    //
    // Arguments:
    // name - the userName of the account
    // pass - the password
    EmailSocket.prototype.auth = function (name, pass) {
        // open the connection on port 110 (POP3)
        if (!this.open (this._host + ":110"))
            return false;
        try {
            // discard the greeting
            var greeting = this.read();
            if (this._debug)
                write ("RECV: " + greeting);
            // issue POP3 commands
            this._POP3 ("USER " + name);
            this._POP3 ("PASS " + pass);
            this._POP3 ("QUIT");
            this.close();
            return true;
        }
        catch (e) {
            this.close();
            return false;
        }
    }
    // Users of the EmailSocket do not need to be concerned with
    // the following method. It is an implementation helper.
    // local function to send a command & check a POP3 reply
    // throws in case of error
    EmailSocket.prototype._POP3 = function (cmd) {
        if (this._debug)
            writeln ("SEND: " + cmd);
        if (!this.writeln (cmd))
            throw "Error";
        var reply = this.read();
        if (this._debug)
            write ("RECV: " + reply);
        // the reply starts by either + or -
        if (reply [0] == "+")
            return;
        throw "Error";
    }
    // Users of the EmailSocket do not need to be concerned with
    // the following method. It is an implementation helper.
    // local function to send a command & check a SMTP reply
    // throws in case of error
    EmailSocket.prototype._SMTP = function (cmd) {
        if (this._debug)
            writeln ("SEND: " + cmd);
        if (!this.writeln (cmd))
            throw "Error";
        var reply = this.read();
        if (this._debug)
            write ("RECV: " + reply);
        // the reply is a three-digit code followed by a space
        var match = reply.match (/^(\d{3})\s/m);
        if (match.length == 2) {
            var n = Number (match [1]);
            if (n >= 200 && n <= 399)
                return;
        }
        throw "Error";
    }
    // nice to have: a toString()
    // This function allows the email object to be printed.
    EmailSocket.prototype.toString = function() {
        return "[object Email]";
    }